#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'optparse'
require 'securerandom'

require_relative 'lib/common'
require_relative '../lib/mq_client'

opt = {}

options = OptionParser.new do |opts|
  opts.banner = 'Usage: multi-docker -n -c -q'

  opts.separator ''
  opts.on('-n HOSTNAME_PREFIX', '--name HOSTNAME_PREFIX', 'format: $tbox_group.$HOSTNAME') do |name|
    opt['hostname_prefix'] = name
  end

  opts.on('-c count', '--count count', 'how many containers do you need') do |num|
    opt['nr_container'] = num
  end

  opts.on('-q queues', '--queues queues', 'separated by ","') do |queues|
    opt['queues'] = queues
  end

  opts.on('-i index', '--index index', 'index of multi-qemu, used for safe-stop') do |index|
    opt['index'] = index
  end

  opts.on_tail('-h', '--help', 'show this message') do
    puts opts
    exit
  end
end

if ARGV.size.zero?
  puts options
  exit 1
end

options.parse!(ARGV)

HOSTNAME = opt['hostname_prefix'] || "dc-1g.#{ENV['HOSTNAME']}"
NR_DC = opt['nr_container'] || 1
QUEUES = opt['queues'] || "dc-1g.#{ENV['HOSTNAME']}"
INDEX = opt['index']

UUID = SecureRandom.uuid
SUITE_FILE = "/tmp/#{ENV['HOSTNAME']}/suite"
SAFE_STOP_FILE = "/tmp/#{ENV['HOSTNAME']}/safe-stop"
RESTART_FILE = "/tmp/#{ENV['HOSTNAME']}/restart/#{UUID}"
RESTART_LOCK_FILE = "/tmp/#{ENV['HOSTNAME']}/restart/lock"

SCHED_HOST = ENV['LKP_SERVER'] || ENV['SCHED_HOST'] || '172.17.0.1'
SCHED_PORT = ENV['LKP_CGI_PORT'] || ENV['SCHED_PORT'] || '3000'
MQ_HOST = ENV['MQ_HOST'] || ENV['LKP_SERVER'] || 'localhost'
MQ_PORT = ENV['MQ_PORT'] || 5672

def start
  reboot_thr = Thread.new do
    loop_reboot_testbox(HOSTNAME, 'dc', MQ_HOST, MQ_PORT)
  end

  threads = {}
  NR_DC.to_i.times do |i|
    thr = Thread.new do
      loop_main("#{HOSTNAME}-#{i}", thr)
    end
    threads[i] = thr
  end

  manage_thr = Thread.new do
    manage_multi_qemu_docker(threads.merge({ 'manage' => manage_thr }), MQ_HOST, MQ_PORT)
  end

  threads.each do |_, thr|
    thr.join
  end

  manage_thr.exit
  reboot_thr.exit
  puts 'all threads exit'

  safe_stop
  puts "#{UUID} exit"
end

def loop_main(hostname, thr)
  loop do
    begin
      thr.exit if File.exist?(SAFE_STOP_FILE)
      system(
        { 'hostname' => hostname, 'queues' => QUEUES, 'uuid' => UUID, 'index' => INDEX },
        ENV['CCI_SRC'] + '/providers/docker.rb'
      )
    rescue StandardError => e
      puts e.backtrace
      # if an exception occurs, request the next time after 30 seconds
      sleep 25
    ensure
      sleep 5
    end
  end
end

if $PROGRAM_NAME == __FILE__
  save_running_suite
  start
end
